/*
 Copyright 2007 Ole Rogeberg

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
 */
package org.lifeyears.web.action;

import org.lifeyears.model.User;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

/**
 * Action regarding the first welcome page, and logging in
 *
 * @author Aleksander Blomskøld
 */
@Scope( "prototype" )
@Component
public class LoginAction
    extends AbstractLifeyearsAction
{

    private String username;

    private String password;

    private String login;

    public void setLogin( final String login )
    {
        this.login = login;
    }

    public int getLifeyearsSaved()
    {
        // FIXME
        return 100;
    }

    public double getLifeyearsSavedPerSecond()
    {
        // FIXME
        return 1.27;
    }

    @Override
    public String execute()
        throws Exception
    {
        final User user = userService.getUserByEmailAndPassword( username, password );

        if ( user == null )
        {
            addActionError( getText( "login.incorrectUsernameOrPassword" ) );
            return ERROR;
        }

        // login succeeded
        session.put( "userId", user.getId() );
        return SUCCESS;
    }

    public String getUsername()
    {
        return username;
    }

    public void setUsername( final String username )
    {
        this.username = username;
    }

    public String getPassword()
    {
        return password;
    }

    public void setPassword( final String password )
    {
        this.password = password;
    }

}
